package com.zhongxi.guoyuan.view.user;

import android.util.Log;
import android.view.View;
import android.webkit.WebView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.consumerYield.ConsumeRule;
import com.zhongxi.guoyuan.entity.consumerYield.ConsumeRuleResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ConsumeRegularActivity extends BaseActivity implements View.OnClickListener {

    private ConsumeRuleResult result;
    private String type;
    private ConsumeRule resultData;
    private WebView webView;


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_consume_regular);
        initTitle();
        tvTitle.setText("收益说明");
        webView= (WebView) findViewById(R.id.consume_rule_webview);

    }

    @Override
    protected void initData() {
        super.initData();
        type = "rule";
        getServer(Url.CONSUME_RULE, null, "", false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
        if (result.getCode() == 1000) {
            showShortToast("参数错误！");
        }
    }

    @Override
    protected void processSuccessResult(String response) throws JSONException {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            if ("rule".equals(type)) {
                result = gson.fromJson(response, ConsumeRuleResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                resultData = result.getData();
                Log.d("flag", "processSuccessResult: "+resultData.toString());
                // showPopupWindow();
                webView.loadDataWithBaseURL(null, getNewContent(resultData.getDescription()), "text/html", "utf-8", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

    private String getNewContent(String htmltext) {

        String html = "<meta name=\"viewport\" content=\"width=device-width\"><div><p>";


        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");

        }
        return doc.toString();
    }
}
